﻿CREATE TRIGGER [dbo].[tru_CarInfo_BookValueCurrent]
ON dbo.t_CarInfo 
FOR UPDATE
AS

IF @@ROWCOUNT = 0 RETURN
IF NOT UPDATE(BookValueCurrent) RETURN

UPDATE CI SET BookValueCurrentDate = GETDATE()
FROM t_CarInfo CI INNER JOIN inserted i ON CI.VIN = i.VIN

INSERT INTO t_BookValueHistory(VIN,Date,CarAge,Value,ValueTypeID)
SELECT i.VIN,GETDATE(),dbo.ModelAge(C.Year,GETDATE()),BookValueCurrent,BookValueCurrentTypeID
FROM inserted i INNER JOIN t_Car C ON i.VIN = C.VIN
